package com.dqhl.business.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.business.R;
import com.dqhl.business.modle.GoodsManager_flag;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.ViewHolder;

import org.xutils.x;

import java.util.List;

/**
 * Created by guanluocang on 2016/11/9.
 */
public class GoodsManagerFlagAdapter extends BaseAdapter{

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsManager_flag> falgList;
    private OnOpenCloseListener mOnOpenCloseListener;
    public GoodsManagerFlagAdapter(Context context, List<GoodsManager_flag> falgList, OnOpenCloseListener onOpenCloseListener){
        this.context = context;
        this.falgList = falgList;
        this.mOnOpenCloseListener = onOpenCloseListener;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return falgList.size();
    }

    @Override
    public Object getItem(int i) {
        return falgList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if(view == null){
            view = inflater.inflate(R.layout.layout_goodsmanager_flag,viewGroup,false);

        }
        ImageView iv_goodsPic = ViewHolder.get(view,R.id.iv_goodsPic);
        TextView tv_goodsName = ViewHolder.get(view,R.id.tv_goodsName);
        TextView tv_goodsNum = ViewHolder.get(view,R.id.tv_goodsNum);
        TextView tv_goodsPrice = ViewHolder.get(view,R.id.tv_goodsPrice);
        ImageView iv_goodsOperation = ViewHolder.get(view,R.id.iv_goodsOperation);
        GoodsManager_flag goodsFlag = falgList.get(i);
        x.image().bind(iv_goodsPic, Config.img_url+goodsFlag.getPic_a());
        tv_goodsName.setText(goodsFlag.getName());
        tv_goodsNum.setText(goodsFlag.getStock());
        tv_goodsPrice.setText(goodsFlag.getPrice());
        if("1".equals(goodsFlag.getOpen())){
            iv_goodsOperation.setImageResource(R.drawable.ic_goodsclose);
        }else{
            iv_goodsOperation.setImageResource(R.drawable.ic_goods_open);
        }
        iv_goodsOperation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnOpenCloseListener.onOpenOrClose(i);
            }
        });
        return view;
    }

    public interface OnOpenCloseListener{
        void onOpenOrClose(int position);
    }
}
